package com.jiamao.goplay.ui.user;

import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.UserPaifLayoutBinding;
import com.jiamao.goplay.ui.user.vm.UserPaifVM;
import com.jiamao.goplay.util.StringUtils;
import com.netease.nim.uikit.api.NimUIKit;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;


/**
 * 配对
 */
public class UserPairFragment extends BaseMVVMFragment<UserPaifLayoutBinding, UserPaifVM> {



    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.user_paif_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.refreshEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                binding.refreshLayout.finishRefresh();
                binding.refreshLayout.finishLoadMore();
            }
        });
        viewModel.uc.refreshNums.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                TextView titleTv =  binding.getRoot().findViewById(R.id.title_right);
                titleTv.setText("共"+ StringUtils.formatBigNum(aBoolean)+"个配对");
            }
        });
        viewModel.uc.startChat.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String aBoolean) {

                NimUIKit.startP2PSession(getActivity(),aBoolean);
            }
        });
        super.initViewObservable();
    }
    @Override
    public UserPaifVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserPaifVM.class);
    }


    @Override
    public void initData() {
        super.initData();

        TextView tv=(TextView)binding.getRoot().findViewById(R.id.title_right);
        String str="共0个配对";
        tv.setText(Html.fromHtml(str));

        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();
        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("配对");
        binding.getRoot().findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDataList(0);
            }
        });
        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDataList(1);
            }
        });

       // binding.userGotoListview.setEmptyView(binding.idEmptyView);
        binding.refreshLayout.autoRefresh();

       // binding.userGotoListview.useDefaultLoadMore(); // 使用默认的加载更多的View。
       // binding.userGotoListview.setLoadMoreListener(mLoadMoreListener); // 加载更多的监听。


        final RefreshLayout refreshLayout = binding.refreshLayout;
//设置 Header 为 贝塞尔雷达 样式
     //   refreshLayout.setRefreshHeader(new BezierRadarHeader(getContext()).setEnableHorizontalDrag(true));
//设置 Footer 为 球脉冲 样式
    //    refreshLayout.setRefreshFooter(new BallPulseFooter(getContext()).setSpinnerStyle(SpinnerStyle.Scale));
        //refreshLayout.setEnableLoadMore(false);//是否启用上拉加载功能
       // refreshLayout.finishLoadMore(false);//结束加载（加载失败）
      //  refreshLayout.finishLoadMoreWithNoMoreData();
    }


    SwipeRecyclerView.LoadMoreListener mLoadMoreListener = new SwipeRecyclerView.LoadMoreListener() {
        @Override
        public void onLoadMore() {
            // 该加载更多啦。
            Log.d("HTML","mLoadMoreListener");
            // 数据完更多数据，一定要调用这个方法。
            // 第一个参数：表示此次数据是否为空。
            // 第二个参数：表示是否还有更多数据。
            //binding.userGotoListview.loadMoreFinish(true, true);

            // 如果加载失败调用下面的方法，传入errorCode和errorMessage。
            // errorCode随便传，你自定义LoadMoreView时可以根据errorCode判断错误类型。
            // errorMessage是会显示到loadMoreView上的，用户可以看到。
            // mRecyclerView.loadMoreError(0, "请求网络失败");
        }
    };
}
